package com.zzyl.job;

import cn.hutool.json.JSONUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.CacheConstants;
import com.zzyl.entity.AlertRule;
import com.zzyl.mapper.AlertRuleMapper;
import com.zzyl.service.AlertRuleSecvice;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.AlertRuleVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AlertJob  {
    @Autowired
    AlertRuleMapper alertRuleMapper;
    @Autowired
    AlertRuleSecvice alertRuleSecvice;
    @Autowired
    RedisTemplate redisTemplate;

    @Scheduled(cron = "0 * * * * ? ")
    public void alertJob() {
        //获取所有规则
      List<AlertRule> alertRules = alertRuleMapper.selectAll(null,null,null);
      if(ObjectUtil.isEmpty(alertRules)){
          return;
      }
        //获取所有数据
        List<Object> values = redisTemplate.opsForHash().values(CacheConstants.DEVICE_LIST);
      if(ObjectUtil.isEmpty(values)){
          return;
      }
        //格式化数据
        List<DeviceDataVo> DeviceDataVos = new ArrayList<>();
       values.forEach(value -> {
           DeviceDataVos.addAll(JSONUtil.toList(JSONUtil.toJsonStr(value),DeviceDataVo.class));
       });
        //循环规则
        DeviceDataVos.forEach(DeviceDataVo -> {
            alertRuleSecvice.alertRuleData(DeviceDataVo);
        });
    }

}
